package com.hongfeng.support.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.AutoBrand;
import com.hongfeng.support.model.AutoType;
import com.hongfeng.support.model.Product;
import com.hongfeng.support.service.AutoBrandService;
import com.hongfeng.support.service.AutoTypeService;
import com.hongfeng.support.service.ProductService;
import com.hongfeng.support.service.common.impl.GenericServiceImpl;

@Service
public class AutoBrandServiceImpl extends GenericServiceImpl<AutoBrand, Long>
		implements AutoBrandService {

	@Resource
	private AutoTypeService autoTypeService;
	@Resource
	private ProductService productService;

	public AutoBrandServiceImpl() {
		super(AutoBrand.class);
	}

	@Override
	public void delete(Long id) {
		ParmItem item = new ParmItem("autoBrand.id", id, FieldOperator.EQUAL);
		List<AutoType> autoTypes = autoTypeService.findAllByQuery(
				AutoType.class, item);
		for (AutoType autoType : autoTypes) {
			item = new ParmItem("autoType.id", autoType.getId(),
					FieldOperator.EQUAL);
			List<Product> products = productService.findAllByQuery(
					Product.class, item);
			for (Product product : products) {
				product.setAutoType(null);
				productService.update(product);
			}
			autoType.setAutoBrand(null);
			autoTypeService.update(autoType);
		}
		super.delete(id);
	}

}
